//
//  GPSDBService.m
//  EYuecheNew
//
//  Created by Ray on 12/22/15.
//  Copyright © 2015 Siloon.KevinW. All rights reserved.
//

#import "GPSDBService.h"

@implementation GPSDBService

+ (GPSDBService *)getInstance
{
    static GPSDBService *service;
    if (service == nil) {
        service = [[GPSDBService alloc] init];
    }
    return service;
}

- (id)init
{
    self = [super init];
    if (self) {
        [self initDatabase];
        return self;
    }
    return nil;
    
}

- (void)initDatabase
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"gps" ofType:@"db"];
    _db = [FMDatabase databaseWithPath:path];
    dbQueue = [FMDatabaseQueue databaseQueueWithPath:path];
}


- (CLLocationCoordinate2D)adjustCoordinate:(CLLocationCoordinate2D)coordinate
{
    CLLocationCoordinate2D adjustCoordinate = coordinate;
    if ([_db open]) {
        int tenLat = (int)(coordinate.latitude * 10);
        int tenLon = (int)(coordinate.longitude * 10);
        FMResultSet *rs = [_db executeQuery:@"SELECT offLat,offLog FROM gpsT WHERE lat= ? AND log = ?",[NSNumber numberWithInt:tenLat],[NSNumber numberWithInt:tenLon]];
        while ([rs next]) {
            NSString *offLat = [rs stringForColumn:@"offLat"];
            NSString *offLong = [rs stringForColumn:@"offLog"];
            adjustCoordinate.latitude = coordinate.latitude + [offLat integerValue] * 0.0001;
            adjustCoordinate.longitude = coordinate.longitude + [offLong integerValue] * 0.0001;
        }
        [rs close];
    }
    return adjustCoordinate;
}



@end
